
package com.anzym.android.kegdroidkey;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.anzym.android.kegdroidlibrary.models.Beer;
import com.anzym.android.kegdroidlibrary.models.BeerOrder;

public class VolumeSelectionDialog extends Dialog {

    private static final String TAG = VolumeSelectionDialog.class.getSimpleName();
    private KegDroidKeyActivity kyA;
    private KegDroidKeyApplication keyApp;
    

    Beer beer;
    View volumeSelector;
    private RadioGroup beerSelectorGroup;
    private RadioButton firstSelectorButton;
    private RadioButton secondSelectorButton;
    private RadioButton thirdSelectorButton;
    private Button orderButton;
    private Button cancelButton;
//    AlphaAnimation alphaAniIn;
//    AlphaAnimation alphaAniOut;

    private TextView beerName;
    private TextView beerABV;
    private TextView beerIBU;
    // private TextView beerVolumeRemaining;
    private ImageView beerImage;
    private BeerOrder beerOrder;
    private BeerOrderDialog bod;

    //int tapNumber;
    int volumeToPour = 0;
    //String androidId;

    public VolumeSelectionDialog(KegDroidKeyActivity ma, Beer b, BeerOrderDialog bod) {
        super(ma);
        this.kyA = ma;
        this.beer = b;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.bod = bod;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vsf_container);
        keyApp = KegDroidKeyApplication.getInstance();
        beerName = (TextView) findViewById(R.id.beer_item_name);
        beerABV = (TextView) findViewById(R.id.beer_item_abv);
        beerIBU = (TextView) findViewById(R.id.beer_item_ibu);
        // beerVolumeRemaining (TextView) findViewById(R.id.;
        beerImage = (ImageView) findViewById(R.id.beer_item_image);

        keyApp.getBeerOrder().setOrderSize(keyApp.MEDIUM_GLASS);
        beerName.setText(beer.getName());
        beerABV.setText("ABV: " + new Double(beer.getAbv()).toString());
        beerIBU.setText("IBU: " + new Integer(beer.getIbu()).toString());
        beerImage.setImageBitmap(Bitmap.createScaledBitmap(beer.getImage(),
                100, 200, false));

        firstSelectorButton = (RadioButton) findViewById(R.id.first_selector_button);
        secondSelectorButton = (RadioButton) findViewById(R.id.second_selector_button);
        thirdSelectorButton = (RadioButton) findViewById(R.id.third_selector_button);

        beerSelectorGroup = (RadioGroup) findViewById(R.id.beer_selector_group);
        beerSelectorGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.first_selector_button:
                        volumeToPour = keyApp.SMALL_GLASS;
                        break;
                    case R.id.second_selector_button:
                        volumeToPour = keyApp.MEDIUM_GLASS;
                        break;
                    case R.id.third_selector_button:
                        volumeToPour = keyApp.LARGE_GLASS;
                        break;
                }

                
                //keyApp.getBeerOrder().setBeerId(beer.getId());
                keyApp.getBeerOrder().setOrderSize(volumeToPour);
                //keyApp.getBeerOrder().setTapNumber(tapNumber);
                //keyApp.getBeerOrder().setAndroidId(androidId);
                
                Log.d(TAG, "Beer ID: " + keyApp.getBeerOrder().getBeerId());
                Log.d(TAG, "Selected: " + keyApp.getBeerOrder().getOrderSize() + " oz.");
                Log.d(TAG, "TapNumber: " + keyApp.getBeerOrder().getTapNumber());
                Log.d(TAG, "KegDroid ID: " + keyApp.getBeerOrder().getAndroidId());
            }

        });

        orderButton = (Button) findViewById(R.id.order_button);
        orderButton.setOnClickListener(new OrderBeerClickListener());

        cancelButton = (Button) findViewById(R.id.cancel_button);
        cancelButton.setOnClickListener(new CancelClickListener());

        
    }

    private class OrderBeerClickListener implements View.OnClickListener {
        public void onClick(View v) {

            Log.d(TAG, "Ordering");
            Log.d(TAG, "Beer ID: " + keyApp.getBeerOrder().getBeerId());
            Log.d(TAG, "Selected: " + keyApp.getBeerOrder().getOrderSize() + " oz.");
            Log.d(TAG, "TapNumber: " + keyApp.getBeerOrder().getTapNumber());
            Log.d(TAG, "KegDroid ID: " + keyApp.getBeerOrder().getAndroidId());
            kyA.beamToKegDroid();
            bod.dismiss();
            VolumeSelectionDialog.this.dismiss();
        }
    }

    private class CancelClickListener implements View.OnClickListener {
        public void onClick(View v) {
            if (((Button) v).isPressed()) {
                Log.d(TAG, "Canceling ");
                VolumeSelectionDialog.this.dismiss();
            }
        }
    }

    
}
